﻿XIncludeFile "Pwnd.pb"

Global GdipWindow.l   ;窗口句柄管理列表


Global Im_WndEvent.l
Global Im_EventHwnd.l
Global Im_EventType.l
Global Im_EventGadget.l
Global Im_EventMenu.l

Structure ARGB
  r.a
  g.a;
  b.a
  a.a;
EndStructure
Structure  LongPoint
  l.l;
  StructureUnion
    y.w
    x.w
    EndStructureUnion
  EndStructure

;取低位 X
Macro LOWORD(Long)   
  Long & $FFFF
EndMacro
 
 ;取高位 Y
Macro HIWORD(Long) 
  Long >> 16 & $FFFF
EndMacro 
  
 ;将座标X,Y转换成一个长整型
Procedure LongPoint(X,Y)
  point. LongPoint ;
  point\x = x
  point\y=y
  ProcedureReturn point\l
EndProcedure

;设置字体
Macro SET_FONT(SText,nText,nX,nY,nWidth,nHeiht,nFontSize=14,nTextColor=$FF000000,nPenWidth=0,nPenColor=$FF000000)
  *Pext.Text=@SText
  *Pext\Text=nText
  *Pext\FontName="宋体"
  *Pext\FontSize=nFontSize
  *Pext\TextColor=nTextColor
  *Pext\PenWidth=nPenWidth
  *Pext\PenColor=nPenColor
  *Pext\Align= #GD_TextLineAlignCenter
  *Pext\Style=4
  *Pext\X=nX
  *Pext\Y=nY
  *Pext\Width=nWidth
  *Pext\Height=nHeiht
EndMacro

;-> 判断数据取值
Procedure IsVal(Val,IFVal)  
 al=(Val ! IFVal) | IFVal
 If Val= al
   ProcedureReturn 1
 EndIf 
EndProcedure

;读取指定颜色中的透明分量
Procedure GdipAlpha(Color)
  *c.ARGB = @Color
  ProcedureReturn *c\a
EndProcedure

;添加一个窗口链表
Procedure AddWindowList(*Host,WndStruLenght)
  *Window.PWnd=PeekL(*Host)
  If *Window
     While *Window\DownList
       *Window=*Window\DownList
     Wend
     *Window\DownList=AllocateMemory(WndStruLenght)
     UpList=*Window
     *Window=*Window\DownList
     *Window\UpList=UpList
  Else 
     *Window=AllocateMemory(WndStruLenght)
     PokeL(*Host,*Window)
  EndIf 
  
  ProcedureReturn *Window
EndProcedure

;从链表中删除指定窗口
Procedure DeleteWindowList(*Address.PWnd)
  If *Address\UpList
    *UpList.PWnd=*Address\UpList
    *UpList\DownList=*Address\DownList
  Else 
    *UpList.PWnd=*Address\HostGadget
    *UpList\Gadget=*Address\DownList 
  EndIf 
  If *Address\DownList
    *DownList.PWnd=*Address\DownList
    *DownList\UpList=*Address\UpList
  Else 
    *UpList.PWnd=*Address\HostGadget
    *UpList\NewGadget=*Address\UpList
  EndIf 
  FreeMemory(*Address)
EndProcedure

;从控件获得
Procedure GadgetGetWindowID(*Gadget.PWnd)
 While *Gadget\Type<>#Window
   *Gadget=*Gadget\HostGadget
 Wend
 ProcedureReturn *Gadget
EndProcedure

Procedure SetText(*Hwnd.PWnd,Text.s)
 *Hwnd\wText(*Hwnd,Text)
 SendMessage_(WindowID(PeekL(GadgetGetWindowID(*Hwnd)+SizeOf(PWnd))),#WM_Draw,*Hwnd,@*Hwnd\X)
EndProcedure

Procedure.s GetText(*Hwnd.PWnd) 
 ProcedureReturn *Hwnd\Creat\Text\Text
EndProcedure

Procedure GetWindow(Hwnd)  ;根据窗句柄获得GDIP窗口指针
  *Window.PWnd=GdipWindow
  While *Window
    If IsWindow(PeekL(*Window+SizeOf(PWnd))) And WindowID(PeekL(*Window+SizeOf(PWnd)))=Hwnd
      ProcedureReturn *Window
    EndIf 
    *Window=*Window\DownList
  Wend 
EndProcedure

Procedure AddWndEvetn(Hwnd,EventGadget,EventType,Type=#EventGadget)
   Im_WndEvent = Type
   Im_EventHwnd =GetWindow(Hwnd)
   Im_EventType = EventType
   If Type=#EventGadget
     Im_EventGadget = EventGadget
   Else 
     Im_EventMenu = EventGadget
   EndIf 
EndProcedure

Procedure ImWndEvent()
  temp.l = Im_WndEvent;
  Im_WndEvent =0
  ProcedureReturn temp
EndProcedure

Procedure ImEventHwnd()
  Temp = Im_EventHwnd
  Im_EventHwnd = 0
  ProcedureReturn Temp
EndProcedure

Procedure ImEventType()
  Temp =Im_EventType
  Im_EventType = 0
  ProcedureReturn  Temp 
EndProcedure

Procedure ImEventGadget()
  Temp =Im_EventGadget
  Im_EventGadget =0
  ProcedureReturn Temp 
EndProcedure

Procedure ImEventMenu()
  Temp =Im_EventMenu
  Im_EventMenu= 0
  ProcedureReturn Temp 
EndProcedure
; IDE Options = PureBasic 5.11 (Windows - x64)
; CursorPosition = 15
; FirstLine = 2
; Folding = 1G+-
; EnableAsm
; EnableUnicode
; EnableXP
; HideErrorLog
; CompileSourceDirectory